package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;

public class ItravelqqNewsAnalyse implements AnalyseNews {

	//http://www.yangtse.com/system/2013/05/07/017157418.shtml
	//http://auto.yzwb.net/qchq/201203/t20120310_839868.htm
	private static final String[] regex={
		"http://www.itravelqq.com/[0-9]{4}/[0-9]{4}/[0-9]*.html",
		"http://www.itravelqq.com/[a-z-a-z-a-z-a-z-a-z-a-z-a-z]*/"
	};

	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		String url=urlMeta.getUrl();
		String htmltxt=urlMeta.getHtml();
		Document doc=Jsoup.parse(htmltxt);
		String title=null;
		String content=null;
		Long date=null;
		String author =null;
		title=doc.select("h1").text();	
		content=HtmlCleaner.getContentHtml(url,doc.select("div.entry-content"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div#endtext"));
		}
		date=DateUtils.matchDate(doc.select("time.entry-date").text());
		author=doc.select("div.hd>span:eq(1)").text();
		if (StringUtils.isBlank(title)||date == null) {
		}
		NewsMeta news=new NewsMeta();
		news.setTitle(title);
		news.setDate(date);
		news.setContent(content);
		news.setUrl(url);
		news.setAuthor(author);
		return news;
	}

	public static void main(String[] args) {
		String url="http://www.itravelqq.com/travelling-in-china-cheaper-when-yuan-slides/";
		ItravelqqNewsAnalyse test=new ItravelqqNewsAnalyse();
		url=UrlArgumentTop.FromatUrl(url);
		System.out.println(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=test.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public boolean isNeedUpdate(){
		return false;
	}

}
